// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/**
 * @brief
 *
 * @version  0.1
 * @author   yuanchenxi
 * @date     2025.02.10

 */
#include "Import/BDFImport/BulkData/Load/PLOADParser.h"

#include <algorithm>
#include <cstdint>

#include <boost/algorithm/string.hpp>

#include "Utility/Logging/SGLogger.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Load/IPLOADService.h"

#include "Import/BDFImport/BDFParserBase.h"
BULKDATA_ENTRY_PARSER_REG (PLOAD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;
/*
// file://./../../Doc/MarkDown/BulkData/PLOAD/PLOAD.md>
*/
// 以下需要修改
enum PLOADKey : uint8_t
{
    PLOAD_SID = 2,
    PLOAD_P   = 3,  // Element identification number. (Integer > 0)
    PLOAD_G1  = 4,
    PLOAD_G2  = 5,
    PLOAD_G3  = 6,
    PLOAD_G4  = 7,
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParsePLOAD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::PLOADData tempdata;

    // TODO(bug 001) m_dominId的正确填写，当前默认domainId设置为1
    tempdata.m_domainId = 1;

    // 当前词条的索引，初始值为1
    int curIDKey = 1;

    // 遍历分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 检查当前词条是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;
            continue;  // 如果是空白，跳过当前词条
        }

        // 将词条转换为科学计数法格式

        // 根据当前词条索引，匹配对应的PLOAD属性

        switch (curIDKey)
        {
        case PLOAD_SID:
            // Handle Load set identification number (SID)
            tempdata.m_sId = convertToI64 (curStrKey);
            break;
        case PLOAD_P:
            // Handle Load per unit surface area (P)
            tempdata.m_P = convertToReal (curStrKey);
            break;
        case PLOAD_G1:
            // Handle Grid point G1
            tempdata.m_G[0] = convertToI64 (curStrKey);
            break;
        case PLOAD_G2:
            // Handle Grid point G2
            tempdata.m_G[1] = convertToI64 (curStrKey);
            break;
        case PLOAD_G3:
            // Handle Grid point G3
            tempdata.m_G[2] = convertToI64 (curStrKey);
            break;
        case PLOAD_G4:
            // Handle Grid point G4
            tempdata.m_G[3] = convertToI64 (curStrKey);
            break;
        default:
            // Handle unknown key
            break;
        }

        // 处理完当前词条后，索引递增
        curIDKey++;
    }

    // 将解析好的PLOAD数据添加到数据库服务

    return dbServiceFactory->get<IPLOADService> ()->Add (tempdata);
}